<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <a-form :label-col="{ md: { span: 6 }, sm: { span: 24 } }" :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }">
        <a-row>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="名称">
              <a-input v-model:value.trim="oceanFeeWhere.name" placeholder="请输入" @keyup.enter="oceanFeeReload" allow-clear />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="选择">
              <a-select v-model:value="oceanFeeWhere.sex" placeholder="请选择" @change="oceanFeeReload" allow-clear>
                <a-select-option value="1">1</a-select-option>
                <a-select-option value="2">2</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{ span: 24 }">
              <em></em>
              <a-space>
                <a-button type="primary" @click="oceanFeeReload">查询</a-button>
                <a-button @click="oceanFeeReset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表格 -->
      <ele-pro-table ref="oceanFeeTableRef" row-key="id" :columns="columns" :datasource="datasource" :scroll="{ x: 1000 }">
        <template #toolbar>
          <a-space>
            <a-button type="primary" @click="oceanFeeOpenEdit()" v-any-permission="['order:oceanFee:save']">
              <template #icon>
                <plus-outlined />
              </template>
              <span>新建</span>
            </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="oceanFeeOpenEdit(record)" v-any-permission="['order:oceanFee:update']">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定要删除此项吗？" @confirm="oceanFeeRemove(record)">
                <a class="ele-text-danger" v-any-permission="['order:oceanFee:remove']">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 海运运费添加、修改弹窗 -->
    <ele-modal :width="460" v-model:visible="oceanFeeVisible_modal" :confirm-loading="loading" :title="oceanFeeIsUpdate ? '修改海运运费' : '新建海运运费'"
      :body-style="{ paddingBottom: '8px' }" @ok="oceanFeeSave" :maskClosable="false">
      <a-form ref="oceanFeeFormRef" :model="oceanFeeForm" :rules="oceanFeeRules" :label-col="{ md: { span: 7 }, sm: { span: 24 } }"
        :wrapper-col="{ md: { span: 17 }, sm: { span: 24 } }">
        <a-form-item label="公司id" name="organizationId">
          <a-input allow-clear :maxlength="200" placeholder="请输入公司id" v-model:value="oceanFeeForm.organizationId" autocomplete="off" />
        </a-form-item>
        <a-form-item label="海运id" name="oceanId">
          <a-input allow-clear :maxlength="200" placeholder="请输入海运id" v-model:value="oceanFeeForm.oceanId" autocomplete="off" />
        </a-form-item>
        <a-form-item label="类型" name="type">
          <a-input allow-clear :maxlength="200" placeholder="请输入类型" v-model:value="oceanFeeForm.type" autocomplete="off" />
        </a-form-item>
        <a-form-item label="费用名称" name="feeName">
          <a-input allow-clear :maxlength="200" placeholder="请输入费用名称" v-model:value="oceanFeeForm.feeName" autocomplete="off" />
        </a-form-item>
        <a-form-item label="金额" name="price">
          <a-input allow-clear :maxlength="200" placeholder="请输入金额" v-model:value="oceanFeeForm.price" autocomplete="off" />
        </a-form-item>
        <a-form-item label="模块 运费,提进,其他,单项,采购,报关费,易港通,财务报销" name="feeModules">
          <a-input allow-clear :maxlength="200" placeholder="请输入模块 运费,提进,其他,单项,采购,报关费,易港通,财务报销" v-model:value="oceanFeeForm.feeModules" autocomplete="off" />
        </a-form-item>
        <a-form-item label="抬头" name="invoicename">
          <a-input allow-clear :maxlength="200" placeholder="请输入抬头" v-model:value="oceanFeeForm.invoicename" autocomplete="off" />
        </a-form-item>
        <a-form-item label="是否开票:开票1 不开票0  " name="invoicestate">
          <a-input allow-clear :maxlength="200" placeholder="请输入是否开票:开票1 不开票0  " v-model:value="oceanFeeForm.invoicestate" autocomplete="off" />
        </a-form-item>
        <a-form-item label="币种" name="currency">
          <a-input allow-clear :maxlength="200" placeholder="请输入币种" v-model:value="oceanFeeForm.currency" autocomplete="off" />
        </a-form-item>
        <a-form-item label="汇率" name="exchangeRate">
          <a-input allow-clear :maxlength="200" placeholder="请输入汇率" v-model:value="oceanFeeForm.exchangeRate" autocomplete="off" />
        </a-form-item>
        <a-form-item label="备注" name="remark">
          <a-input allow-clear :maxlength="200" placeholder="请输入备注" v-model:value="oceanFeeForm.remark" autocomplete="off" />
        </a-form-item>
      </a-form>
    </ele-modal>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { toDateString } from 'ele-admin-pro';
import useFormData from '@/utils/use-form-data';
import { message } from 'ant-design-vue';

import { pageOceanFee, removeOceanFee, addOceanFee, updateOceanFee } from '@/api/order/oceanFee';

// 表格实例
const oceanFeeTableRef = ref(null);

// 表格列配置
const columns = ref([
  {
    key: 'index',
    width: 48,
    align: 'center',
    fixed: 'left',
    hideInSetting: true,
    customRender: ({ index }) => index + (oceanFeeTableRef.value?.tableIndex ?? 0)
  },
  {
    title: '海运id',
    dataIndex: 'oceanId',
    key: 'oceanId',
    // sorter: true
  },
  {
    title: '类型',
    dataIndex: 'type',
    key: 'type',
    // sorter: true
  },
  {
    title: '费用名称',
    dataIndex: 'feeName',
    key: 'feeName',
    // sorter: true
  },
  {
    title: '金额',
    dataIndex: 'price',
    key: 'price',
    // sorter: true
  },
  {
    title: '模块 运费,提进,其他,单项,采购,报关费,易港通,财务报销',
    dataIndex: 'feeModules',
    key: 'feeModules',
    // sorter: true
  },
  {
    title: '抬头',
    dataIndex: 'invoicename',
    key: 'invoicename',
    // sorter: true
  },
  {
    title: '是否开票:开票1 不开票0  ',
    dataIndex: 'invoicestate',
    key: 'invoicestate',
    // sorter: true
  },
  {
    title: '币种',
    dataIndex: 'currency',
    key: 'currency',
    // sorter: true
  },
  {
    title: '汇率',
    dataIndex: 'exchangeRate',
    key: 'exchangeRate',
    // sorter: true
  },
  {
    title: '备注',
    dataIndex: 'remark',
    key: 'remark',
    // sorter: true
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '创建人',
    dataIndex: 'createName',
    key: 'createName',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    key: 'updateTime',
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '更新人',
    dataIndex: 'updateName',
    key: 'updateName',
  },
  {
    title: '操作',
    key: 'action',
    width: 200,
    align: 'center',
    fixed: 'right',
    hideInSetting: true
  }
]);

// 表单数据
const { form: oceanFeeWhere, resetFields } = useFormData({
  username: '',
  sex: undefined
});

// 表格数据源
const datasource = ({ page, limit, where, orders }) => {
  return pageOceanFee({ ...where, ...orders, page, limit });
};

/* 搜索 */
const oceanFeeReload = () => {
  oceanFeeTableRef?.value?.reload({ page: 1, where: oceanFeeWhere });
};

/*  重置 */
const oceanFeeReset = () => {
  resetFields();
  oceanFeeReload();
};

/* 删除单个 */
const oceanFeeRemove = (record) => {
  const hide = message.loading('请求中..', 0);
  removeOceanFee(record.id).then((msg) => {
    hide();
    message.success(msg);
    oceanFeeReload();
  }).catch((e) => {
    hide();
    message.error(e.message);
  });
};

// 当前选中数据
const oceanFeeCurrent = ref(null);

// 弹窗是否打开
const oceanFeeVisible_modal = ref(false);

// 是否是修改
const oceanFeeIsUpdate = ref(false);

// 提交状态
const loading = ref(false);

//
const oceanFeeFormRef = ref(null);

// 表单数据
const { form: oceanFeeForm, resetFields: oceanFeeResetFormFields } = useFormData({
  id: undefined,
  customer: '',
  vessel: '',
});

// 表单验证规则
const oceanFeeRules = reactive({
  name: [
    {
      required: true,
      message: '请输入名称',
      type: 'string',
    }
  ],
});

/* 打开编辑弹窗 */
const oceanFeeOpenEdit = (record) => {
  oceanFeeResetFormFields();
  oceanFeeFormRef.value?.clearValidate();
  oceanFeeCurrent.value = record?? {};
  oceanFeeVisible_modal.value = true;
  Object.assign(oceanFeeForm, record ?? {});
  oceanFeeIsUpdate.value = !!record;
};

/* 保存编辑 */
const oceanFeeSave = () => {
  if (!oceanFeeFormRef.value) {
    return;
  }
  oceanFeeFormRef.value.validate().then(() => {
    loading.value = true;
    const saveOrUpdate = oceanFeeIsUpdate.value ? updateOceanFee : addOceanFee;
    saveOrUpdate(oceanFeeForm).then((msg) => {
      loading.value = false;
      message.success(msg);
      oceanFeeVisible_modal.value = false;
      oceanFeeReload();
    }).catch((e) => {
      loading.value = false;
      message.error(e.message);
    });
  }).catch(() => {
  });
};
</script>

<script>
export default {
  name: 'DemoTest'
};
</script>

